package com.ruoyi.web.controller.myController;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;

import com.ruoyi.system.domain.*;
import com.ruoyi.system.domain.vo.*;
import com.ruoyi.system.service.*;
import com.ruoyi.system.service.impl.SelectUser;
import com.ruoyi.web.controller.utils.RedisUtil;
import com.ruoyi.web.controller.utils.SendMail;
import com.ruoyi.web.controller.utils.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import com.ruoyi.framework.web.service.TokenService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
public class RegisterController {
    @Autowired
    private UserService userService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private SelectUser selectUser;
    @Autowired
    private SendMail sendMail;
    @Autowired
    private IMyuserService userMapper;
    @Autowired
    private IUserinfoService userinfoMapper;
    @Autowired
    private IWebService webMapper;
    @Autowired
    private IJwebService jwebMapper;
    @Autowired
    private IUserskillService userskillMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private RedisService redisService;
    @PostMapping("/api/regist")
    public Results regist(@RequestBody LoginDTO loginDTO){
        String s = UUID.randomUUID().toString();
        Myuser uer = userMapper.selectMyuserByName(loginDTO.getLoginName());
        if(uer==null) {
            String code =(String) redisUtil.get(loginDTO.getLoginName());
            if(loginDTO.getMycode().equals(code)) {
                Myuser user = new Myuser(loginDTO.getLoginName(),s.substring(0,6), loginDTO.getPassword(),loginDTO.getEmail(),1l);
                userMapper.insertMyuser(user);
                Myuser myuser = userMapper.selectMyuserByName(loginDTO.getLoginName());
                LoginVO loginVO = new LoginVO();
                loginVO.setId(Integer.valueOf(myuser.getUserid().toString()));
                LoginUser loginUser = new LoginUser();
                loginUser.setUserId(myuser.getUserid());
                SysUser sysUser = new SysUser();
                sysUser.setUserName(myuser.getUserName());
                sysUser.setPassword(myuser.getPassword());
                loginUser.setUser(sysUser);
                User users = new User(user.getUserid(),null,user.getUserName(),user.getLoginName(),user.getPassword(),user.getEmail(),1,user.getNdate(),user.getNdate());
                userService.saveUser(users);
                String token = tokenService.createToken(loginUser);
                loginVO.setToken(token);
                loginVO.setUsername(user.getUserName());
                loginVO.setImage("https://images.starbin.cn/img/%E5%A4%B4%E5%83%8F1.jpg");
                Integer loginTotalCount =(Integer) redisService.get("LoginTotalCount");
                if(loginTotalCount==null){
                    redisService.set("LoginTotalCount",1);
                }else{
                    redisService.incr("LoginTotalCount",1);
                }
                Integer loginCount =(Integer) redisService.get("LoginCount");
                if(loginCount==null){
                    redisService.set("LoginCount",1);
                }else{
                    redisService.incr("LoginCount",1);
                }
                return new Results(200, "", loginVO);
            }
            else{
                return new Results(401,"验证码错误或者过期","");
            }
        }
        else{
            return new Results(400,"该用户名已经存在","");
        }
    }
    @PostMapping("/api/getCode")
    public Results getCode(@RequestBody LoginDTO loginDTO){
        String loginName = loginDTO.getLoginName();
        Myuser user = selectUser.getUser(loginName);
        if(user==null) {
            String email = loginDTO.getEmail();
            Myuser myuser = userMapper.selectMyuserByEmail(email);
            if(myuser!=null){
                return new Results(111,"该邮箱已经注册过了！","");
            }
            String s = "";
            Random random = new Random();
            for (int i = 0; i < 6; i++) {
                s = s + String.valueOf(random.nextInt(10));
            }
            sendMail.send(s,email);
            redisUtil.set(loginName,s,120);
            return new Results(200,"获取验证码成功",s);
        }else{
            return new Results(400,"用户名已存在","");
        }
    }
    @PostMapping("/api/selectSkill")
    public table select(@RequestBody Userinfo skill){
        Userinfo userinfo = userinfoMapper.selectUserinfoByUid(skill.getUid());
        List<Web> webs = null;
        List<Jweb> jwebs = null;
        List<SkillTable> list = new ArrayList<SkillTable>();
        if(userinfo==null) {
            userinfo=new Userinfo(skill.getUid(),skill.getSex(),skill.getSkill());
            userinfoMapper.insertUserinfo(userinfo);
            Long skill1 = skill.getSkill();
            if (skill1 == 1l) {
                webs = webMapper.selectWebList(1l);
                for(Web web:webs){
                    SkillTable skillTable = new SkillTable(Integer.parseInt(web.getWid().toString()),Integer.parseInt(web.getInfoid().toString()),"前端",web.getName());
                    list.add(skillTable);
                }
            }
            if (skill1 == 2l) {
                jwebs = jwebMapper.selectJwebList(skill1);
                for(Jweb jweb:jwebs){
                    SkillTable skillTable = new SkillTable(Integer.parseInt(jweb.getJid().toString()),Integer.parseInt(jweb.getInfoid().toString()),"后端",jweb.getName());
                    list.add(skillTable);
                }
            }
            if (skill1 == 3l) {
                webs = webMapper.selectWebList(1l);
                for(Web web:webs){
                    SkillTable skillTable = new SkillTable(Integer.parseInt(web.getWid().toString()),Integer.parseInt(web.getInfoid().toString()),"前端",web.getName());
                    list.add(skillTable);
                }
                jwebs = jwebMapper.selectJwebList(2l);
                for(Jweb jweb:jwebs){
                    SkillTable skillTable = new SkillTable(Integer.parseInt(jweb.getJid().toString()),Integer.parseInt(jweb.getInfoid().toString()),"后端",jweb.getName());
                    list.add(skillTable);
                }
            }
        }
        else{
            Long skill1 = skill.getSkill();
            if (skill1 == 1l) {
                webs = webMapper.selectWebList(1l);
                for(Web web:webs){
                    SkillTable skillTable = new SkillTable(Integer.parseInt(web.getWid().toString()),Integer.parseInt(web.getInfoid().toString()),"前端",web.getName());
                    list.add(skillTable);
                }
            }
            if (skill1 == 2) {
                jwebs = jwebMapper.selectJwebList(skill1);
                for(Jweb jweb:jwebs){
                    SkillTable skillTable = new SkillTable(Integer.parseInt(jweb.getJid().toString()),Integer.parseInt(jweb.getInfoid().toString()),"后端",jweb.getName());
                    list.add(skillTable);
                }
            }
            if (skill1 == 3) {
                webs = webMapper.selectWebList(1L);
                for(Web web:webs){
                    SkillTable skillTable = new SkillTable(Integer.parseInt(web.getWid().toString()),Integer.parseInt(web.getInfoid().toString()),"前端",web.getName());
                    list.add(skillTable);
                }
                jwebs = jwebMapper.selectJwebList(2l);
                for(Jweb jweb:jwebs){
                    SkillTable skillTable = new SkillTable(Integer.parseInt(jweb.getJid().toString()),Integer.parseInt(jweb.getInfoid().toString()),"后端",jweb.getName());
                    list.add(skillTable);
                }
            }
        }
        return new table(Integer.parseInt(skill.getUid().toString()),list);
    }
    @PostMapping("/api/toInfo")
    public Results toInfo(@RequestBody ListUserSkill listUserSkill){
        List<Userskill> userskill = userskillMapper.selectUserskillByUserid(listUserSkill.getUserid().longValue());
        List<SkillTable> skillTable = listUserSkill.getSkillTable();
        Myuser myuser = userMapper.selectMyuserByUserid(listUserSkill.getUserid().longValue());
        User userByName = userService.getUserByName(myuser.getLoginName());
        userByName.setType(listUserSkill.getSkill());
        userService.updateUser(userByName);

        if(userskill.isEmpty()) {
            for(SkillTable user : skillTable)
            {
                Userskill userskill1 = new Userskill(listUserSkill.getUserid().longValue(), listUserSkill.getSkill().longValue(),user.getId().longValue(),user.getInfoid().longValue(),user.getName());
                userskillMapper.insertUserskill(userskill1);
            }
            return new Results(200, "信息注册成功", "");
        }
        else{
            return new Results(505,"该信息已经存在了","");
        }
    }
    @GetMapping("/api/getSkill/{userid}/{skillinfo}")
    public  Results getSkill(@PathVariable Long userid,@PathVariable("skillinfo") Long skillinfo){
        List<Userskill> userskills = new ArrayList<>();
        if(skillinfo==3){
            List<Userskill> userskill1 = userskillMapper.selectUserskillList(userid,1l);
            List<Userskill> userskill2 = userskillMapper.selectUserskillList(userid,2l);
            userskills.addAll(userskill1);
            userskills.addAll(userskill2);
            if(userskills==null){
                return new Results(500,"查询技能失败","");
            }
        }else{
            userskills = userskillMapper.selectUserskillList(userid,skillinfo);
        if(userskills==null){
            return new Results(500,"查询技能失败","");
        }
        }
        return new Results(400,"",userskills);
    }
    @GetMapping("/api/getSkill/{userid}")
    public  Results getSkillByUserId(@PathVariable Long userid){
        List<Userskill> userskills = userskillMapper.selectUserskillByUserid(userid);
        return new Results(400,"",userskills);
    }
}
